package com.zhiyou100.taxi.acction;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import com.zhiyou100.taxi.dao.CarMapper;
import com.zhiyou100.taxi.entity.Car;
import com.zhiyou100.taxi.service.CarService;
import com.zhiyou100.taxi.utils.FileNameUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import sun.java2d.pipe.SpanIterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-24 21:42
 * @Description
 */
@Controller
@RequestMapping("/car")
public class CarController {
    @Autowired
    CarService carService;

    @RequestMapping("/show.action")
    public String show() {
        return "forward:/car/allPage.action";
    }

    @RequestMapping("/allPage.action")
    public String getCarPage(Integer pageNum, HttpServletRequest request) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<Car> pageInfo = carService.queryPageCar(pageNum);
        request.getSession().setAttribute("carPageInfo", pageInfo);
        return "carList";
    }

    @RequestMapping("deleteOne.action")
    public String deleteOne(String carId) {
        int num = carService.deleteByPrimaryKeyOne(carId);
        return "forward:/car/allPage.action";
    }

    //异步上传的图片的名称
    String saveFilename = "";

    @RequestMapping("/save.action")
    public String saveCar(Car car, HttpServletRequest request) {
        String primaryKey = carService.getPrimaryKey();
        car.setCarId(primaryKey);
        car.setCarPhoto("/imgs/car/" + saveFilename);
        int num = carService.saveOne(car);
        if (num > 0) {
            request.setAttribute("msg", "增加成功");
        } else {
            request.setAttribute("msg", "增加失败");
        }
        //只是添加商品时清空 saveFileName,如果再次访问 异步ajax就不会为空
        //清空 saveFileName 内容 为了下次新增或者修改的异步 ajax的上传处理
        saveFilename = "";
        //增加成功后应该重新访问数据库,所以跳转分页显示的 action上
        return "forward:/car/allPage.action";
    }

    @RequestMapping("/getOne.action")
    public String getOneCar(String carId, HttpServletRequest request) {
        Car car = carService.getCarByPrimaryKeyOne(carId);
        request.setAttribute("car", car);
        return "carview/car-edit";
    }

    @RequestMapping("/update.action")
    public String update(Car car, HttpServletRequest request) {
        if (!saveFilename.equals("")) { //如果为空说明修改过了
            car.setCarPhoto("/imgs/car/" + saveFilename);
        }
        int num = carService.updateCarOne(car);
        if (num > 0) {
            request.setAttribute("msg", "更新成功");
        } else {
            request.setAttribute("msg", "更新失败");
        }
        //增加成功后应该重新访问数据库,所以跳转分页显示的 action上
        return "forward:/car/allPage.action";
    }

    @RequestMapping("/deleteBatch.action")
    public String deleteBath(String carIds, HttpServletRequest request) {
        String[] split = carIds.split(",");
        HashSet<String> set = new HashSet<>();
        set.addAll(Arrays.asList(split));
        int num = carService.deleteByPrimarySet(set);
        return "forward:/car/allPage.action";
    }

    //ajax 页面回显图片
    @ResponseBody
    @RequestMapping("/ajaxImg.action")
    public Object ajaxImg(MultipartFile carPhoto, HttpServletRequest request) {//pimage 要和 图片上传页面的image标签的 name属性一致
        //1. 提取生成文件名 uuid + 上传图片的后缀

        saveFilename = FileNameUtil.getUUIDFileName() + FileNameUtil.getFileType(carPhoto.getOriginalFilename());
        System.out.println("保存的图片====>saveFilename = " + saveFilename);
        //2. 得到项目中图片存储的路径
        String path = request.getServletContext().getRealPath("/imgs/car");
        //3. 转存
        //D:\code\mimissm\image_big\4321898f83924.png
        try {
            carPhoto.transferTo(new File(path + File.separator + saveFilename));

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("图片路径===》path = " + path + "\\" + saveFilename);
        //返回客户端 json对象,封装图片的路径,为了在页面实现立即回显
        JSONObject object = new JSONObject();
        //放入上传图片的名称
        object.put("imgurl", saveFilename);
        //如果 json对象返回必须 toString,转换特点
        return object.toString();
    }
}
